package com.xiaxueqi.web.shop;

import java.util.ArrayList;
import java.util.List;

import com.xiaxueqi.entity.book.OrderDetail;

public class ShopCart {
	private List<OrderDetail> buyList;
	private List<OrderDetail> deleteList;
	private Integer amount;
	private Double totalCost;

	public ShopCart() {
		this.buyList = new ArrayList<OrderDetail>();
		this.deleteList = new ArrayList<OrderDetail>();
		this.amount = 0;
		this.totalCost = Double.valueOf(0);
	}

	public void add(OrderDetail oneProduct) {
		Integer i1 = oneProduct.getProductId();
		int i;
		for (i = 0; i < buyList.size(); i++) {
			OrderDetail temp = buyList.get(i);
			Integer i2 = temp.getProductId();
			if (i1.equals(i2)) {
				temp.setQuantity(temp.getQuantity() + oneProduct.getQuantity());
				break;
			}
		}
		if (i == buyList.size()) {
			buyList.add(oneProduct);
		}
		this.amount = this.amount + oneProduct.getQuantity();
		this.totalCost = this.totalCost + oneProduct.getUnitPrice()
				* oneProduct.getQuantity();
	}

	/**
	 * 增加已有商品的数量1
	 * 
	 * @param productId
	 */
	public void add(Integer productId) {
		OrderDetail temp = new OrderDetail();
		for (int i = 0; i < buyList.size(); i++) {
			temp = buyList.get(i);
			Integer i2 = temp.getProductId();
			if (productId.equals(i2)) {
				buyList.get(i).setQuantity(temp.getQuantity() + 1);
				break;
			}
		}
		amount++;
		this.totalCost = this.totalCost + temp.getUnitPrice();
	}

	/**
	 * 当某一件商品存在时，对一件商品增加n数量
	 * 
	 * @param productId
	 * @param quantity
	 */
	public void add(Integer productId, Integer quantity) {
		try {
			OrderDetail temp = new OrderDetail();
			int i = 0;
			for (i = 0; i < buyList.size(); i++) {
				temp = buyList.get(i);
				Integer i2 = temp.getProductId();
				if (productId.equals(i2)) {
					buyList.get(i).setQuantity(quantity + temp.getQuantity());
					break;
				}
			}
			if (i == buyList.size())
				throw new Exception("没有此商品！");

			amount = amount + quantity;
			this.totalCost = this.totalCost + temp.getUnitPrice() * quantity;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 当某一件商品存在时，直接设置其数量
	 * 
	 * @param productId
	 * @param quantity
	 */
	public void set(Integer productId, Integer quantity) {
		try {
			OrderDetail temp = new OrderDetail();
			int i = 0;
			Integer j = 0;
			for (i = 0; i < buyList.size(); i++) {
				temp = buyList.get(i);
				Integer i2 = temp.getProductId();
				if (productId.equals(i2)) {
					j = temp.getQuantity();
					buyList.get(i).setQuantity(quantity);
					break;
				}
			}
			if (i == buyList.size())
				throw new Exception("没有此商品！");

			// System.out.println(temp.getQuantity());// 指针指向，所以会变
			amount = amount - j + quantity;
			this.totalCost = this.totalCost + temp.getUnitPrice()
					* (quantity - j);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除一件商品全部数量
	 * 
	 * @param oneProduct
	 */
	public void remove(OrderDetail oneProduct) {
		for (int i = 0; i < buyList.size(); i++) {
			OrderDetail temp = buyList.get(i);
			Integer i2 = temp.getProductId();
			if (oneProduct.getProductId().equals(i2)) {
				deleteList.add(buyList.remove(i));
				break;
			}
		}
		this.amount = this.amount - oneProduct.getQuantity();
		this.totalCost = this.totalCost - oneProduct.getUnitPrice()
				* oneProduct.getQuantity();
	}

	/**
	 * 减少已有商品的数量1
	 * 
	 * @param productId
	 */
	public void remove(Integer productId) {
		OrderDetail temp = new OrderDetail();
		for (int i = 0; i < buyList.size(); i++) {
			temp = buyList.get(i);
			Integer i2 = temp.getProductId();
			if (temp.getQuantity() == 1) {
				deleteList.add(buyList.remove(i));
				break;
			}
			if (productId.equals(i2)) {
				buyList.get(i).setQuantity(temp.getQuantity() - 1);
				break;
			}
		}
		amount--;
		this.totalCost = this.totalCost - temp.getUnitPrice() * 1;
	}

	public void change(Integer productId) {
		OrderDetail temp = new OrderDetail();
		for (int i = 0; i < deleteList.size(); i++) {
			temp = deleteList.get(i);
			Integer i2 = temp.getProductId();
			if (productId.equals(i2)) {
				this.add(deleteList.remove(i));
				break;
			}
		}
	}

	public OrderDetail getOneProduct(Integer productId) {
		OrderDetail temp = new OrderDetail();
		for (int i = 0; i < buyList.size(); i++) {
			temp = buyList.get(i);
			Integer i2 = temp.getProductId();
			if (productId.equals(i2)) {
				temp = buyList.get(i);
				return temp;
			}
		}
		return null;
	}

	public List<OrderDetail> getBuyList() {
		return buyList;
	}

	public void setBuyList(List<OrderDetail> buyList) {
		this.buyList = buyList;
	}

	public List<OrderDetail> getDeleteList() {
		return deleteList;
	}

	public void setDeleteList(List<OrderDetail> deleteList) {
		this.deleteList = deleteList;
	}

	public Integer getAmount() {
		return amount;
	}

	public void setAmount(Integer amount) {
		this.amount = amount;
	}

	public Double getTotalCost() {
		return totalCost;
	}

	public void setTotalCost(Double totalCost) {
		this.totalCost = totalCost;
	}

	public static void main(String[] args) {
		Integer i1 = new Integer(1);
		Integer i2 = new Integer(1);
		System.out.println(i1 == i2);
	}
}
